<template>
  <div class="doc-detail-container">
    <search-bar></search-bar>
    <div class="detail-box">
      <img class="cover" :src="cover" />
      <div class="doc-info">
        <h1>{{ label }}</h1>
        <div class="brief">{{ brief }}</div>
        <div class="data">
          <span>{{ readings }}浏览</span>
          <span>{{ like }}点赞</span>
          <span>{{ favnum }}收藏</span>
          <span>{{ downloads }}下载</span>
          <span>{{ date }}上传</span>
          <span>{{ type }}</span>
        </div>
      </div>
      <div class="button-group">
        <div @click="favClick()"><img :src="getFavIcon()" />收藏</div>
        <div @click="updateLike()"><img :src="getLikeIcon()" />点赞</div>
        <div @click="fileDownload(fileId, label, type)">
          <img :src="getDownloadIcon()" />下载
        </div>
      </div>
    </div>
    <el-dialog title="添加到收藏夹" :visible.sync="dialogVisible" width="440px">
      <div class="favorites-group">
        <form
          class="new-favorite-form"
          v-if="isEditing"
          @submit.prevent="onSubmit()"
        >
          <input
            type="text"
            v-model="newFavName"
            onblur="if(this.value==''){this.value='输入收藏夹名';}"
            onfocus="if(this.value=='输入收藏夹名'){this.value='';}"
          />
          <button>新建</button>
        </form>
        <div class="new-favorite-button" v-else @click="isEditing = !isEditing">
          <img src="@/assets/image/docDetail/add.png" />新建收藏夹
        </div>
        <div
          class="favorite"
          v-for="item in favoriteItems"
          :key="item.boxroomId"
          @click="addToFav(item.boxroomId)"
        >
          {{ item.boxroomName }}
          <span>收藏</span>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getFavListData,
  getIsFav,
  getIsLike,
  updateFileLike,
  addToFavorite,
  createNewFav,
  getFileInfoData,
  getPreviewImage,
  getFileDownload,
} from "@/api/detail/detail";
import SearchBar from "@/components/Frame/SearchBar/SearchBar.vue";

export default {
  components: { SearchBar },
  inject: ["reload"],
  data() {
    return {
      cover: "",
      label: "",
      brief: "",
      readings: 0,
      favnum: 0,
      like: 0,
      downloads: 0,
      date: "",
      type: "",
      fileId: 103,
      userId: this.$store.getters.userId,
      newFavName: "",
      isFavActive: false,
      isLikeActive: false,
      isDownloadActive: false,
      favicon: require("@/assets/image/docDetail/favorite.png"),
      likeicon: require("@/assets/image/docDetail/like.png"),
      downloadicon: require("@/assets/image/docDetail/download.png"),
      dialogVisible: false,
      isEditing: false,
      favoriteItems: [],
    };
  },
  watch: {
    "$route.query.id": function () {
      this.reload();
    },
  },
  methods: {
    // 下载文件
    fileDownload(fileId, fileName, fileSuffix) {
      let data = {
        userId: this.userId,
        fileId: fileId,
      };
      getFileDownload(data)
        .then((res) => {
          const blob = new Blob([res], { type: " application/octet-stream" });
          if ("download" in document.createElement("a")) {
            //非Ie下载
            let fileLink = document.createElement("a");
            let fileURL = window.URL.createObjectURL(blob);
            fileLink.href = fileURL;
            fileLink.setAttribute("download", fileName + "." + fileSuffix);
            document.body.appendChild(fileLink);
            fileLink.click();
            window.URL.revokeObjectURL(fileLink.href); //释放URL对象
            document.body.removeChild(fileLink);
            this.isDownloadActive = true;
          } else {
            navigator.msSaveBlob(blob, fileName);
          }
          console.log(res);
          if (res === "") window.alert("你下载的文件丢失了");
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //获取文件详情信息
    getFileInfo() {
      let data = {
        fileId: this.fileId,
        userId: this.userId,
      };
      getFileInfoData(data).then((res) => {
        if (res.code === 200) {
          //获取文字信息
          this.label = res.data.fileName;
          this.brief = res.data.fileIntro;
          this.readings = res.data.browseNum;
          this.favnum = res.data.collectionNum;
          this.like = res.data.likeNum;
          this.downloads = res.data.downloadNum;
          this.date = res.data.createTime;
          this.type = res.data.fileSuffix;
          //获取封面
          getPreviewImage(this.fileId).then((res) => {
            this.cover = "data:image/png;base64," + res;
          });
          //获取点赞、收藏状态
          let body = { userId: this.userId, fileId: this.fileId };
          getIsFav(body).then((res) => {
            this.isFavActive = res.isCollection;
          });
          getIsLike(body).then((res) => {
            this.isLikeActive = res.isLike;
          });
        } else {
          this.$message({
            message: res.msg,
            center: true,
            type: "error",
          });
        }
      });
    },
    //点赞按钮点击事件
    updateLike() {
      let body = { userId: this.userId, fileId: this.fileId };
      updateFileLike(body).then((res) => {
        if (res.code === 200) this.isLikeActive = !this.isLikeActive;
      });
    },
    //收藏按钮点击事件
    favClick() {
      this.dialogVisible = true;
      this.getFavoriteItems();
    },
    //控制不同状态下收藏、点赞、下载按钮颜色
    getFavIcon() {
      if (this.isFavActive)
        return require("@/assets/image/docDetail/favorite-true.png");
      else return require("@/assets/image/docDetail/favorite.png");
    },
    getLikeIcon() {
      if (this.isLikeActive)
        return require("@/assets/image/docDetail/like-true.png");
      else return require("@/assets/image/docDetail/like.png");
    },
    getDownloadIcon() {
      if (this.isDownloadActive)
        return require("@/assets/image/docDetail/download-true.png");
      else return require("@/assets/image/docDetail/download.png");
    },
    //获取收藏夹列表
    getFavoriteItems() {
      let data = { userId: this.userId };
      getFavListData(data).then((res) => {
        this.favoriteItems = res.data;
      });
    },
    //将文件添加进点击文件夹
    addToFav(favId) {
      let data = {
        userId:this.userId,
        boxroomId: favId,
        fileId: this.fileId,
      };
      addToFavorite(data).then((res) => {
        console.log(res.code);
        if (res.code === 200) {
          this.$message({
            message: res.msg,
            center: true,
            type: "success",
          });
          this.isFavActive = true;
        } else {
          this.$message({
            message: res.msg,
            center: true,
            type: "error",
          });
        }
      });
    },
    //新建收藏夹
    onSubmit() {
      let data = {
        boxroomName: this.newFavName,
        userId: this.userId,
      };
      let body = JSON.stringify(data);
      createNewFav(body).then((res) => {
        if (res.code === 200) {
          this.getFavoriteItems();
          this.newFavName = "";
          this.isEditing = false;
        } else {
          this.$message({
            message: res.msg,
            center: true,
            type: "error",
          });
          this.newFavName = "";
        }
      });
    },
  },
  created() {
    this.fileId = this.$route.query.id;
    this.getFileInfo();
  },
};
</script>

<style scoped>
.doc-detail-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  background-image: url("@/assets/image/common/background/background.png");
  background-size: 100% 100%;
  padding-top: 80px;
  box-sizing: border-box;
}
.detail-box {
  width: 960px;
  height: 432px;
  margin-top: 80px;
  padding: 48px 64px;
  box-sizing: border-box;
  border-radius: 12px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  background-color: rgb(250, 250, 250);
}
.cover {
  width: 150px;
  height: 210px;
  border: 1px solid rgba(0, 0, 0, 0.19);
  float: right;
}
.doc-info {
  height: 210px;
  width: 640px;
  float: left;
  text-align: left;
}
.brief {
  height: 104px;
  width: 100%;
  margin-top: 12px;
  color: rgb(120, 120, 120);
  word-break: break-all;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
}
.data {
  margin-top: 32px;
}
.data > span {
  color: rgb(120, 120, 120);
  margin-right: 12px;
}
.button-group {
  height: 40px;
  width: 600px;
  margin-top: 272px;
  box-sizing: border-box;
}
.button-group img {
  height: 28px;
  width: 28px;
  vertical-align: text-bottom;
  margin-right: 8px;
}
.button-group div {
  float: left;
  cursor: pointer;
  margin-right: 32px;
  box-sizing: border-box;
  height: 40px;
  line-height: 40px;
  font-size: 24px;
}
.favorites-group {
  width: 400px;
  min-height: 320px;
  box-sizing: border-box;
  padding: 0 20px;
}
.new-favorite-button {
  cursor: pointer;
  width: 100%;
  height: 32px;
  box-sizing: border-box;
  padding: 4px 0;
  margin-bottom: 16px;
  text-align: left;
  font-size: 16px;
}
.new-favorite-button > img {
  width: 16px;
  height: 16px;
  margin-right: 8px;
  margin-top: 4px;
  float: left;
}
.new-favorite-button:hover,
.favorite:hover {
  background-color: rgba(0, 0, 0, 0.09);
}
.favorite {
  cursor: pointer;
  width: 100%;
  height: 32px;
  box-sizing: border-box;
  padding: 4px 4px 4px 24px;
  margin-bottom: 2px;
  text-align: left;
  font-size: 16px;
}
.favorite > span {
  height: 24px;
  width: 48px;
  float: right;
  font-size: 14px;
  text-align: center;
  line-height: 24px;
  box-sizing: border-box;
  border-radius: 16px;
  border: 1px solid rgba(0, 0, 0, 0.19);
}
.new-favorite-form {
  height: 32px;
  width: 100%;
  border-radius: 16px;
  margin-bottom: 16px;
  border: 1px solid rgba(0, 0, 0, 0.19);
}
.new-favorite-form > input {
  height: 95%;
  width: 80%;
  border: none;
  box-sizing: border-box;
  border-top-left-radius: 16px;
  border-bottom-left-radius: 16px;
  padding-left: 12px;
  color: #797979;
}
.new-favorite-form > input:focus {
  outline: none;
  color: black;
}
.new-favorite-form > button {
  height: 100%;
  width: 20%;
  border-top-right-radius: 16px;
  border-bottom-right-radius: 16px;
  border: none;
  background-color: white;
  border-left: 1px solid rgba(0, 0, 0, 0.19);
}
</style>
